<?php

namespace Modules\Coupon\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Coupon\Entities\UserCoupon;

use Validator;
use ThrowException;
use Auth;

class MyCouponController extends Controller
{
    // 我的优惠券列表
    public function index(Request $req)
    {
        $rule = [
            'type' => ['required', 'in:expired,used,valid']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $options = $req->all();

        $options['with'] = ['base_coupon'];

        $type = $req->type;
        $options['builder'] = $user->coupons()->$type();

        $options['only'] = [
            'id',
            'uuid',
            'usable_start_at',
            'usable_end_at',
            'base_coupon' => [
                'title',
                'description',
                'usable_range_type',
                'money_limit_type',
                'minimum_money',
                'discount_type',
                'discount_money',
                'discount_rate',
                'discount_money_limit',
                'usable_range_text',
                'to_use_link'
            ]
        ];

        $list = UserCoupon::getList($options);

        return $list->generateListResponse();
    }
}
